简介

etcd 是一个 golang 编写的分布式、高可用的一致性键值存储系统,用于提供可靠的分布式键值(key-value)存储、配置共享和服务发现等功能。etcd可以用于存储关键数据和实现分布式调度,在现代化的集群运行中能够起到关键性的作用。

etcd基于 raft协议,通过复制日志文件的方式来保证数据的强一致性。在etcd之前,常用的是基于paxos协议的zookeeper。

单机安装etcd

etcd在生产环境中一般推荐集群方式部署。在这里,主要讲讲单节点安装和基本使用。

CentOS

1
yum install -y etcd

启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 设置开机自启动
$ systemctl enable etcd
# 启动etcd
$ systemctl start etcd
# 查看etcd运行状态
$ systemctl status etcd
● etcd.service - Etcd Server
Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-05-24 23:08:57 CST; 7s ago
Main PID: 20272 (etcd)
Tasks: 8
Memory: 10.0M
CGroup: /system.slice/etcd.service
└─20272 /usr/bin/etcd --name=default --data-dir=/var/lib/etcd/default.etcd --listen-client-urls=http://localhost:2379
May 24 23:08:57 VM_0_3_centos etcd[20272]: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
May 24 23:08:57 VM_0_3_centos etcd[20272]: 8e9e05c52164694d became leader at term 2
May 24 23:08:57 VM_0_3_centos etcd[20272]: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
May 24 23:08:57 VM_0_3_centos etcd[20272]: setting up the initial cluster version to 3.3
May 24 23:08:57 VM_0_3_centos etcd[20272]: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
May 24 23:08:57 VM_0_3_centos etcd[20272]: ready to serve client requests
May 24 23:08:57 VM_0_3_centos etcd[20272]: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
May 24 23:08:57 VM_0_3_centos systemd[1]: Started Etcd Server.
May 24 23:08:57 VM_0_3_centos etcd[20272]: set the initial cluster version to 3.3
May 24 23:08:57 VM_0_3_centos etcd[20272]: enabled capabilities for version 3.3

systemd配置

systemctl status etcd 命令的输出可以看到,etcd的 systemd配置文件位于 /usr/lib/systemd/system/etcd.service,该配置文件的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target

从上面可以看出,etcd的配置文件: EnvironmentFile=-/etc/etcd/etcd.conf。如果我们想要修改某些配置项,可以编辑该文件。

远程访问

etcd安装完成后,默认只能本机访问,如果想要其他主机访问,还需要修改 /etc/etcd/etcd.conf 中的配置。例如,本实例安装etcd的机器IP是 127.0.0.1(下面服务器ip用127.0.0.1替代,自己根据实际更改),在自己的机器远程访问服务器上安装的etcd的2379端口,结果访问被拒绝:

1
2
3
4
$ telnet 127.0.0.1 2379
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host

修改 /etc/etcd/etcd.conf 配置:

注意:此处 127.0.0.1 使用内网ip

1
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://localhost:2379"

重启etcd

1
$ systemctl restart etcd

etcdctl 常用命令

查看版本

1
2
3
4
etcdctl -v
etcdctl version: 3.3.11
API version: 2

可以看到默认 API version: 2,由于etcdctl的版本2和版本3并不兼容,我们使用以下命令设置。

1
2
3
4
5
6
# 设置环境变量,版本3。
export ETCDCTL_API=3
etcdctl version
etcdctl version: 3.3.11
API version: 3.3

COMMANDS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
get Gets the key or a range of keys
put Puts the given key into the store
del Removes the specified key or range ofkeys [key, range_end)
txn Txn processes all the requests in onetransaction
compaction Compacts the event history in etcd
alarm disarm Disarms all alarms
alarm list Lists all alarms
defrag Defragments the storage of the etcdmembers with given endpoints
endpoint health Checks the healthiness ofendpoints specified in `--endpoints` flag
endpoint status Prints out the status ofendpoints specified in `--endpoints` flag
endpoint hashkv Prints the KV history hash foreach endpoint in --endpoints
move-leader Transfers leadership to another etcdcluster member.
watch Watches events stream on keys orprefixes
version Prints the version of etcdctl
lease grant Creates leases
lease revoke Revokes leases
lease timetolive Get lease information
lease list List all active leases
lease keep-alive Keeps leases alive (renew)
member add Adds a member into the cluster
member remove Removes a member from the cluster
member update Updates a member in the cluster
member list Lists all members in the cluster
snapshot save Stores an etcd node backendsnapshot to a given file
snapshot restore Restores an etcd member snapshotto an etcd directory
snapshot status Gets backend snapshot statusof a given file
make-mirror Makes a mirror at the destinationetcd cluster
migrate Migrates keys in a v2 store to amvcc store
lock Acquires a named lock
elect Observes and participates in leaderelection
auth enable Enables authentication
auth disable Disables authentication
user add Adds a new user
user delete Deletes a user
user get Gets detailed information of a user
user list Lists all users
user passwd Changes password of user
user grant-role Grants a role to a user
user revoke-role Revokes a role from a user
role add Adds a new role
role delete Deletes a role
role get Gets detailed information of a role
role list Lists all roles
role grant-permission Grants a key to a role
role revoke-permission Revokes a key from a role
check perf Check the performance of the etcdcluster
help Help about any command

查看集群状态和节点

1
etcdctl member list

插入数据

可以把key理解成目录

1
2
3
4
5
6
7
8
etcdctl put /hello/key1 "world1"
OK
etcdctl put /hello/key2 "world2"
OK
etcdctl put /hello/key3 "world3"
OK

根据key查询

1
2
3
4
etcdctl get /hello/key1
/hello/key1
world1

删除一条数据

1
2
3
etcdctl del /hello/key3
1

批量获取目录下的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 获取key value
etcdctl get /hello/ --prefix
# 返回值
/hello/key1
world1
/hello/key2
world2
#只获取key
etcdctl get /hello/ --prefix --keys-only
# 返回值
/hello/key1
/hello/key2
# 只获取value
etcdctl get /hello/ --prefix --print-value-only
# 返回值
world1
world2

watch监听key

1
etcdctl watch /hello/key1

租约(可以理解成定时器,把key绑定上之后,到时间了key就被删除了)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
etcdctl lease grant 60
# 返回值
lease 694d72477c721546 granted with TTL(60s)
# 创建一条数据并绑定到上面租约上
etcdctl put --lease=694d72477c721546 /hello/key3 "world3"
# 马上获取数据
etcdctl get /hello/key3
# 可以删除租约(对应的key也会被直接删除)
etcdctl lease revoke 694d72477c721546
# 续租(60s)
etcdctl lease keep-alive 694d72477c721546

snapshot

保存备份当前集群etcd的信息,用于新的etcd集群

1
2
3
4
5
etcdctl snapshot save snapshot.db
# 返回
Snapshot saved at snapshot.db
ls
snapshot.db

访问控制相关

添加用户

1
2
3
4
5
# root用户
etcdctl user add root
# 非root用户
etcdctl user add mochu

添加role

1
etcdctl role add admin

将用户加入角色中

1
etcdctl user grant-role mochu admin

本文只介绍简单的指令,仅供学习使用,更多详细命令,请查看官方文档。